package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.domain.Member;
import com.itheima.domain.Order;
import com.itheima.domain.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.vo.OrderInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 预约订单数据的实现类
 */
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    /**
     * 添加预约订单
     *
     * @param orderInfoVo
     * @return
     */
    @Transactional
    @Override
    public Integer add(OrderInfoVo orderInfoVo) {
        try {
            String orderDate = orderInfoVo.getOrderDate();
            Integer setmealId = orderInfoVo.getSetmealId();
            String telephone = orderInfoVo.getTelephone();
            String idCard = orderInfoVo.getIdCard();
            String name = orderInfoVo.getName();
            String sex = orderInfoVo.getSex();
            OrderSetting orderSetting = orderSettingDao.selectByDate(orderDate);
            if (ObjectUtils.isEmpty(orderSetting)) {
                throw new RuntimeException("预约日期不在工作日内");
            }
            int number = orderSetting.getNumber();
            int reservations = orderSetting.getReservations();
            if (reservations >= number) {
                throw new RuntimeException("预约人数已满");
            }
            Member member = memberDao.selectMemberByPhone(telephone);
            if (ObjectUtils.isEmpty(member)) {
                member = new Member();
                member.setName(name);
                member.setSex(sex);
                member.setPhoneNumber(telephone);
                member.setIdCard(idCard);
                member.setRegTime(new Date());
                memberDao.insertMember(member);
            }
            Order order = orderDao.selectOrderByOrderDate(member.getId(), orderDate, setmealId);
            if (!(ObjectUtils.isEmpty(order))) {
                return order.getId();
            }
            Date date = new SimpleDateFormat("yyyy-MM-dd").parse(orderDate);
            order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(date);
            order.setOrderStatus(Order.ORDERSTATUS_NO);
            order.setOrderType(Order.ORDERTYPE_WEIXIN);
            order.setSetmealId(setmealId);
            orderDao.insertOrder(order);
            orderSettingDao.updateReservationsByDate(orderDate);
            return order.getId();
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("创建预约订单失败", e);
        }
    }


    /**
     * 根据预约订单id查询成功预约后的订单信息
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findAssociateOrderById(Integer id) {
        Map<String, Object> map = orderDao.findAssociateOrderById(id);
        Date orderDate = (Date) map.get("orderDate");
        String orderDateString = new SimpleDateFormat("yyyy-MM-dd").format(orderDate);
        map.put("orderDate",orderDateString);
        return map;
    }
}
